<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;

use Cart;


class LoginController extends Controller
{

    /**
     * 登录页面
     * 
     * [login description]
     * @return [type] [description]
     */
    public function login()
    {

        return view('/home/login/login');
    }

    /**
     *登录验证
     * 
     * [dologin description]
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function dologin(Request $request)
    {
        $username = $request -> input('username');
        $password = $request -> input('password');

        //判断用户名密码是否为空
        if (empty($username | $password)) {
            return back();
        }

        //查询用户是否存在
        $username = DB::select("select * from users where username='$username' or phone='$username'");

        if(empty($username)) {
            return back()->with('info','用户名错误');
        }

        //判断密码是否正确
        $id = $username[0]->id;
        $name = $username[0]->username;
        $headpic = $username[0]->headpic;
        // dd($name);
        if(Hash::check($password,($username[0]->password))){
            
            session(['id'=>$id,'username'=>$name,'headpic'=>$headpic]);
            // session([]);

            // $cart = Cart :: content();
             // $cart = Cart :: content();
             $cart = Cart :: count();
            // dd($cart);
            if($cart){
                 return redirect('/dingdan');
            }else{
                return redirect('/shoplist');
            }

        }else{
            return back()->with('infos','密码错误');
        }

    }

    /**
     *退出登录返回到店铺列表
     * 
     * [logout description]
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function logout(Request $request)
    {
        $request->session()->flush();
        // dd(session('id'));
        return redirect('/');
    }

    /**
     * 注册页面
     * 
     * [register description]
     * @return [type] [description]
     */
    public function register()
    {
        return view('/home/login/register');
    }

    /**
     * 注册写入数据库
     * 
     * [doregister description]
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function doregister(Request $request)
    {


        $phone = $request -> input('phone');

        $password = $request -> input('password');
        $username = str_random(10);

        $this->validate($request, [
              'phone' => 'required|regex:/^\w{6,20}$/|unique:users,phone',
              'password' => 'required|regex:/^\S{6,20}$/|same:repassword'
          ],[
                  'phone.required'=>'对不起手机号必填',
                  'phone.regex'=>'请输入正确手机号',
                  'phone.unique'=>'该手机号已被注册',
                  'password.required'=>'对不起密码必填',
                  'password.regex'=>'密码格式不正确',
                  'password.same'=>'两次密码不同'
          ]);
        
        $users = DB::table('users')->insert([
            'phone'=>"$phone",
            'username'=>"$username",
            'password'=>Hash::make("$password"),
            'headpic'=>'default.jpg'
            ]);
        

        if($users){
            $res = DB::select("select * from users where phone='$phone'")[0]->id;
    
            $accounts = DB::table('accounts')->insert(['uid'=>"$res"]);
                return view('/home/login/login');
            }else{
                
            return back();
        }

    }
}
